package com.zhu.nio.channel;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by ehsy_it on 2016/4/12.
 */
public class FileChannelTruncate {

    public static void main(String[] args) throws IOException {
        RandomAccessFile aFile = new RandomAccessFile("D:/data/nio-data.txt", "rw");
        FileChannel channel = aFile.getChannel();

        channel.truncate(5);

        ByteBuffer buf = ByteBuffer.allocate(20);
        channel.read(buf);
        buf.flip();

        while(buf.hasRemaining()){
            System.out.print((char)buf.get());
        }

        aFile.close();
    }
}
